package com.microsoft;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import com.model.Data;
import com.service.*;

/**
 * Created by sww on 16-5-5.
 */
public class Request {
    //在这个request中返回的最大值被设置为40，这就意味着可能有些正确结果是不会被返回的
    public Data request(long Id, String type){   //type: Id, AA.AuId
        HttpClient httpclient = HttpClients.createDefault();
        String expr_string;
        if(type == "Id"){
            expr_string=type+"="+Id;
        }else{
            expr_string="Composite("+type+"="+Id+")";
        }
        //System.out.print(expr_string);
        try
        {
            URIBuilder builder = new URIBuilder("https://oxfordhk.azure-api.net/academic/v1.0/evaluate");

            //builder.setParameter("expr", "And(Id=2096079112,Y>2012)");
            builder.setParameter("expr", expr_string);
            builder.setParameter("model", "latest");
            builder.setParameter("attributes", "Id,RId,F.FId,C.CId,J.JId,AA.AfId,AA.AuId");
            builder.setParameter("count", "40");
            builder.setParameter("offset", "0");

            URI uri = builder.build();
            HttpGet request = new HttpGet(uri);
            request.setHeader("Ocp-Apim-Subscription-Key", "f7cc29509a8443c5b3a5e56b0e38b5a6");


            // Request body
            //StringEntity reqEntity = new StringEntity("{body}");
            //request.setEntity(reqEntity);
            //request.set

            HttpResponse response = httpclient.execute(request);
            HttpEntity entity = response.getEntity();

            if (entity != null && type == "Id")
            {//json解析过程
                ParseJson parseJson= new ParseJson();
                Data data;  //创建返回的对象
                //System.out.print(EntityUtils.toString(entity));
                data = parseJson.parse(JSONObject.parseObject(EntityUtils.toString(entity)));
                //System.out.println(data.toString());
                return data;
                //System.out.println(sww_test.toString());
                //System.out.println(EntityUtils.toString(entity));
                //System.out.println(JSONObject.toJSON(EntityUtils.toString(entity)));
            }
            return null;
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
            return null;
        }
    }


    //重载函数，用于处理非Id请求的情况，返回的是list,request中返回的最大值被设置为40
    public List<Data> request(long Id, String type, int n){   //type: Id, AA.AuId
        HttpClient httpclient = HttpClients.createDefault();
        String expr_string;
        if(type == "RId"){
            expr_string=type+"="+Id;
        }else {
            expr_string = "Composite(" + type + "=" + Id + ")";
        }
        //System.out.print(expr_string);
        try
        {
            URIBuilder builder = new URIBuilder("https://oxfordhk.azure-api.net/academic/v1.0/evaluate");

            //builder.setParameter("expr", "And(Id=2096079112,Y>2012)");
            builder.setParameter("expr", expr_string);
            builder.setParameter("model", "latest");
            builder.setParameter("attributes", "Id,RId,F.FId,C.CId,J.JId,AA.AfId,AA.AuId");
            builder.setParameter("count", "1000");
            builder.setParameter("offset", "0");

            URI uri = builder.build();
            HttpGet request = new HttpGet(uri);
            request.setHeader("Ocp-Apim-Subscription-Key", "f7cc29509a8443c5b3a5e56b0e38b5a6");


            // Request body
            //StringEntity reqEntity = new StringEntity("{body}");
            //request.setEntity(reqEntity);
            //request.set

            HttpResponse response = httpclient.execute(request);
            HttpEntity entity = response.getEntity();

            if (entity != null && type != "Id")
            {//json解析过程
                List<Data> datalist = new ArrayList<Data>(); //创建返回对象
                ParseAuId parseAuId = new ParseAuId();
                datalist = parseAuId.parse(JSONObject.parseObject(EntityUtils.toString(entity)));
                return datalist;
                //System.out.println(JSONObject.toJSON(EntityUtils.toString(entity)));
            }
            return null;
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
            return null;
        }
    }
}
